﻿<link href="@Url.Content("/content/styles/order.css")" rel="stylesheet" type="text/css" />

<div class="ld-tabs">
    <ul>
        <li class="k-state-active">填写新的 @LuxDiner.Web.Management.Common.GetOrderName(ViewBag.Method)</li>
    </ul>
    <div>
        <div class="k-block form_title">
            <ul>
                @*<li>
                    <b>单据号：<span style="color: red" id="orderNo">@(LuxDiner.Web.Management.Common.GetOrderNo((ViewBag.Method==(int)LuxDiner.Model.OrderMethod.Expenses)?"F":"S"))</span></b>
                </li>*@
                <li>
                    <b>日期：</b><input id="txtNoteDate" />
                </li>
            </ul>
            <ul>
                <li>
                    <b>客户：</b><input id="txtCustomer" data-value="0" style="width: 300px;" />
                </li>
                <li>
                    <b>备注：</b><input class="k-textbox" id="txtDescription" style="width: 300px;" />
                </li>
            </ul>
        </div>
        <div id="grid_orders"></div>
        <table class="tbsum">
            <tr>
                <td style="width: 296px"></td>
                <td style="width: 295px;">&nbsp;&nbsp;总金额：<span id="lblSum"></span></td>
                <td>&nbsp;</td>
            </tr>
        </table>
    </div>
</div>


<script class="init">
    var submenu;
    @if (ViewBag.Method == 21)
    {
        <text>submenu = "新付款单";</text>
    }
    else if (ViewBag.Method == 22)
    {
        <text>submenu = "新收款单";</text>
    }
    LocatedMenu("财务管理", submenu);

    $("#txtNoteDate").kendoDatePicker({
        value: new Date(),
        culture: "zh-CHS",
        format: "yyyy年MM月dd日",
    });
    var allCustomer = Whale.Remote.GetEntity({
        url: "/Members/Customer/Gets",
        parameters: {
            cType: "@(ViewBag.Method % 2 == 0 ? 1 : 2)",
            customerStatus: 1
        }
    });
    $("#txtCustomer").kendoComboBox({
        placeholder: "请选择客户...",
        filter: "contains",
        dataTextField: "PinYinAndMobile",
        template: "#=ComplexName#",
        dataValueField: "ID",
        dataSource: allCustomer,
        change: function (e) {
            if ($.isNumeric(this.value())) {
                var item = this.dataItem();
                $("#txtCustomer").attr("data-value", item.ID);
                this.text(item.ComplexName);
            }
        }
    });
</script>

<script class="grid_orders_columns">
    var orderFields = [{
        title: "往来帐户",
        field: "Bank",
        template: "#=Bank.ShowName#",
        editor: bankEditor,
        width: 300
    }, {
        title: "金额",
        field: "Money",
        format: "{0:c}",
        editor: moneyEditor,
        width: 150
    }, {
        title: "操作",
        command: [{
            name: "destroy",
            text: "删除"
        }]
    }];

    function bankEditor(container, options) {
        var selBank = $('<input required data-text-field="ShowName" data-value-field="ID" data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoDropDownList({
                autoBind: false,
                dataSource: Whale.dataSource({
                    url: "/Financial/Bank/Gets"
                }),
                change: function (e) {
                    if (selBank.dataItem() == null || selBank.dataItem() == undefined) {
                        $("#grid_orders").data("kendoGrid").cancelRow();
                        return;
                    }
                    options.model.Bank = selBank.dataItem();
                },
                dataBound: function (e) {
                    if (selBank.dataItem(0) != null)
                        options.model.Bank = selBank.dataItem(0);
                }
            }).data("kendoDropDownList");
    }

    function moneyEditor(container, options) {
        var txtMoney = $('<input required data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoNumericTextBox({
                format: "{0:c}",
                change: function (e) {
                    ComputeSum();
                }
            }).data("kendoNumericTextBox");
    }

    function ComputeSum() {
        var sum = 0;
        $.each(orderDataSource.data(), function (i, o) {
            sum += o.Money;
        });
        $("#lblSum").html(kendo.toString(sum, "c"));
    }
</script>

<script class="grid_orders_dataSource">
    var orderDataSource = new kendo.data.DataSource({
        schema: {
            model: {
                fields: {
                    Bank: { defaultValue: { ID: 0, ProductName: "请选择往来帐号" } },
                    Money: { type: "number", defaultValue: 0 },
                }
            }
        },
        change: function (e) {
            if (e.action == "remove") {
                ComputeSum();
            }
        }
    });
</script>

<script class="grid_orders_config">
    $("#grid_orders").kendoGrid({
        columns: orderFields,
        pageable: false,
        toolbar: [{
            name: "create",
            text: "新增一行"
        }, {
            name: "save",
            text: "保存单据"
        }],
        editable: true,
        dataBound: function (e) {
            //让表头居中
            $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            $("a.k-grid-save-changes").attr("style", "font-weight:bold; float:right");
        },
        dataSource: orderDataSource,
        saveChanges: function (e) {
            var values = {
                //orderNo: $("#orderNo").html(),
                orderDate: kendo.toString($("#txtNoteDate").data("kendoDatePicker").value(), "yyyy-MM-dd"),
                customerID: $("#txtCustomer").attr("data-value"),
                description: $("#txtDescription").val(),
                method:"@ViewBag.Method",
                orderItems: []
            };
            values.orderItems = $.map(orderDataSource.data(), function (o) {
                return {
                    bankID: o.Bank.ID,
                    money: o.Money
                }
            });
            Whale.Remote.Operation({
                url: "/Orders/PaidOrder/SaveOrders",
                parameters: values,
                success: function (result) {
                    location.href = "/Success/PrintPaidOrder/" + result.OrderNo;
                },
                error: function (msg) {
                    wndGeneralInformation.content(msg).open();
                }
            });
        }
    });
</script>
